function formatNormalize(formatter, isPad) {
  if (typeof formatter === 'function') {
    return formatter
  }
  if (typeof formatter !== 'string') {
    throw new TypeError('formatter must be a string')
  }
  if (formatter == 'date') {
    formatter = 'yyyy-MM-dd'
  } else if (formatter == 'datetime') {
    formatter = 'yyyy-MM-dd HH:mm:ss'
  }
  const formmatterFunc = (dateInfo) => {
    const { yyyy, MM, dd, HH, mm, ss, ms } = dateInfo
    return formatter.replaceAll('yyyy', yyyy)
      .replaceAll('MM', MM)
      .replaceAll('dd', dd)
      .replaceAll('HH', HH)
      .replaceAll('mm', mm)
      .replaceAll('ss', ss)
      .replaceAll('ms', ms)
  }
  return formmatterFunc
}
export function formate(date, formatter, isPad = false) {
  formatter = formatNormalize(formatter)
  const dateInfo = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    mm: date.getMinutes(),
    s: date.getSeconds(),
    ms: date.getMilliseconds(),
  }
  dateInfo.yyyy = dateInfo.y.toString();
  dateInfo.MM = dateInfo.m.toString();
  dateInfo.dd = dateInfo.d.toString();
  dateInfo.HH = dateInfo.h.toString();
  dateInfo.mm = dateInfo.mm.toString();
  dateInfo.ss = dateInfo.s.toString();
  dateInfo.ms = dateInfo.ms.toString();
  function _pad(prop, len) {
    dateInfo[prop] = dateInfo[prop].padStart(len, '0')
  }
  if (isPad) {
    _pad('yyyy', 4)
    _pad('MM', 2)
    _pad('dd', 2)
    _pad('HH', 2)
    _pad('mm', 2)
    _pad('ss', 2)
    _pad('ms', 3)
  }
  const result = formatter(dateInfo)
  // console.log(result)
  return result
}